package com.xusheng.cli.controller.back;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.dictionaryModule.Category;
import com.xusheng.cli.req.CategoryQueryReq;
import com.xusheng.cli.req.SetRankReq;
import com.xusheng.cli.service.CategoryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分类设置相关接口
 */
@RestController
@RequestMapping("/api/v1/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 根据条件查询分类信息
     *
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("")
    public Result<MyPage<Category>> list(CategoryQueryReq req) {
        LambdaQueryWrapper<Category> query = Wrappers.<Category>lambdaQuery()
                .like(StrUtil.isNotBlank(req.getName()), Category::getName, req.getName())
                .gt(req.getStartTime() != null, Category::getCreateTime, req.getStartTime())
                .lt(req.getEndTime() != null, Category::getCreateTime, req.getEndTime())
                .orderByDesc(Category::getCreateTime);
        MyPage<Category> page = categoryService.page(req.getPage(), query);
        return Result.success(page);
    }

    /**
     * 保存分类信息
     *
     * @param category 分类信息
     * @return 返回结果
     */
    @PostMapping("")
    public Result<Object> save(@Validated(Save.class) @RequestBody Category category) {
        if (category.getRanking() != null && category.getRanking()) {
            long count = categoryService.lambdaQuery()
                    .eq(Category::getRanking, true)
                    .count();
            if (count > 3) {
                return Result.error("排行只能设置4个");
            }
        }
        boolean success = categoryService.save(category);
        return Result.isSuccess(success);
    }

    /**
     * 更新分类信息
     *
     * @param category 分类信息
     * @return 返回结果
     */
    @PutMapping("")
    public Result<Object> update(@Validated(Update.class) @RequestBody Category category) {
        if (category.getRanking() != null && category.getRanking()) {
            long count = categoryService.lambdaQuery()
                    .ne(Category::getId, category.getId())
                    .eq(Category::getRanking, true)
                    .count();
            if (count > 3) {
                return Result.error("排行只能设置4个");
            }
        }
        boolean success = categoryService.updateById(category);
        return Result.isSuccess(success);
    }

    /**
     * 根据id获取分类信息
     *
     * @param id 分类编号
     * @return 返回结果
     */
    @GetMapping("/{id}")
    public Result<Category> info(@PathVariable long id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }


    /**
     * 删除分类信息
     *
     * @param id 分类编号
     * @return 返回结果
     */
    @DeleteMapping("/{id}")
    public Result<Object> delete(@PathVariable long id) {
        Category category = categoryService.getById(id);
        return Result.isSuccess(categoryService.removeById(category));
    }

    /**
     * 设置分类排行状态
     *
     * @param req 排行状态
     * @return 返回结果
     */
    @PutMapping("/setRank")
    public Result<Object> setRank(@Validated @RequestBody SetRankReq req) {
        if (req.getRanking()) {
            long count = categoryService.lambdaQuery()
                    .ne(Category::getId, req.getId())
                    .eq(Category::getRanking, true)
                    .count();
            if (count > 3) {
                return Result.error("排行只能设置4个");
            }
        }
        Category category = new Category();
        category.setId(req.getId());
        category.setRanking(req.getRanking());
        boolean success = categoryService.updateById(category);
        return Result.isSuccess(success);
    }

    /**
     * 获取所有分类
     *
     * @return 返回结果
     */
    @GetMapping("/all")
    public Result<List<Category>> allCategory() {
        List<Category> list = categoryService.lambdaQuery()
                .orderByDesc(Category::getCreateTime)
                .list();
        return Result.success(list);
    }
}
